import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../../res/colors.dart';

class CardView extends StatefulWidget {
  const CardView(
      {Key? key,
      this.shadowColor = ColorStyle.commonShadowColor,
      required this.child,
      this.blurRadius,
      this.spreadRadius,
      this.bgColor = Colors.white,
      this.radius,
      this.opacity = 1,
      this.borderRadius,
      this.margin,
      this.padding,
      this.isRepaint = true})
      : super(key: key);

  final Color bgColor; // 底色

  final double? radius;
  final double? opacity; // 透明度 范围： 0-1

  final double? blurRadius; //阴影范围

  final double? spreadRadius; //阴影浓度

  final Color shadowColor; //阴影颜色

  final Widget child;

  final EdgeInsets? margin;

  final EdgeInsets? padding;

  final BorderRadius? borderRadius;
  final bool isRepaint;

  @override
  State<StatefulWidget> createState() {
    return CardViewState();
  }
}

class CardViewState extends State<CardView> {
  late final double radius;
  late final double opacity;
  late final EdgeInsets margin;
  late final EdgeInsets padding;

  @override
  void initState() {
    super.initState();
    opacity = widget.opacity ?? 1.0;
    radius = widget.radius ?? 20.w;
    margin = widget.margin ??
        EdgeInsets.only(
          left: 15.w,
          right: 15.w,
        );
    padding = widget.padding ??
        EdgeInsets.only(
          left: 15.w,
          right: 15.w,
        );
  }

  @override
  Widget build(BuildContext context) {
    return widget.isRepaint ? buildChild() : RepaintBoundary(child: buildChild());
  }

  Widget buildChild() {
    return Opacity(
      //设置透明度
      opacity: opacity,
      child: Container(
        margin: margin,
        padding: padding,
        alignment: Alignment.center,
        decoration: BoxDecoration(
          color: widget.bgColor,
          boxShadow: [
            BoxShadow(
              blurRadius: widget.blurRadius ?? 5.w,
              //阴影范围
              spreadRadius: widget.spreadRadius ?? 0,
              //阴影浓度
              color: widget.shadowColor, //阴影颜色
            ),
          ],
          borderRadius: widget.borderRadius ?? BorderRadius.circular(radius),
        ),
        child: Container(
          // padding: padding,
          child: widget.child,
        ),
      ),
    );
  }
}
